﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using Color = System.Windows.Media.Color;
using AllInOne.Utils;
using ArcSoftFace.Utils;

namespace AllInOne.Pages
{
    /// <summary>
    /// ChoosePhotoPage.xaml 的交互逻辑
    /// </summary>
    public partial class ChoosePhotoPage : Page
    {

        private MainWindow parent;

        public static int HUE;
        public static int BRIGHTNESS;
        public static int SATURATION;


        public ChoosePhotoPage()
        {
            InitializeComponent();
        }

        public ChoosePhotoPage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }



        public void InitPage() {
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));

            checkIndex = -1;
            this.photoList.Clear();
            this.resultPhotoList.Clear();
            this.clearChecked();
            this.imgPhoto1.Visibility = Visibility.Hidden;
            this.imgPhoto2.Visibility = Visibility.Hidden;
            this.imgPhoto3.Visibility = Visibility.Hidden;
        }


        private List<Bitmap> photoList = new List<Bitmap>();
        private List<Bitmap> resultPhotoList = new List<Bitmap>();


        public void AddPhoto(Bitmap photo)
        {
            Bitmap resultPhoto = ImageUtil.ChangeHSL(photo,HUE, BRIGHTNESS, SATURATION);
            resultPhoto.RotateFlip(RotateFlipType.RotateNoneFlipX);
            this.photoList.Add((Bitmap)photo.Clone());
            this.resultPhotoList.Add(resultPhoto);
            if (this.photoList.Count == 1)
            {
                this.imgPhoto1.Source = ImageUtil.BitmapToBitmapImage(photo);
                this.imgPhoto1.Visibility = Visibility.Visible;
            }

            if (this.photoList.Count == 2)
            {
                this.imgPhoto2.Source = ImageUtil.BitmapToBitmapImage(photo);
                this.imgPhoto2.Visibility = Visibility.Visible;
            }

            if (this.photoList.Count ==3)
            {
                this.imgPhoto3.Source = ImageUtil.BitmapToBitmapImage(photo);
                this.imgPhoto3.Visibility = Visibility.Visible;

                this.parent.PlaySound(Properties.Resources.male_choose_photo, Properties.Resources.female_choose_photo);

            }
        }

        
        private int checkIndex = -1;

        private void ImgPhoto1_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.checkIndex = 0;

            this.clearChecked();

            this.brd1.BorderBrush = new SolidColorBrush(Color.FromRgb(167, 255, 82));
            this.imgCheckPhoto1.Visibility = Visibility.Visible;


        }

        private void ImgPhoto2_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.checkIndex = 1;

            this.clearChecked();

            this.brd2.BorderBrush = new SolidColorBrush(Color.FromRgb(167, 255, 82));
            this.imgCheckPhoto2.Visibility = Visibility.Visible;
        }

        private void ImgPhoto3_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.checkIndex = 2;

            this.clearChecked();

            this.brd3.BorderBrush = new SolidColorBrush(Color.FromRgb(167, 255, 82));
            this.imgCheckPhoto3.Visibility = Visibility.Visible;
        }

        private void BtnRetry_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.StartTakePhoto();
        }

        private void BtnSubmit_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (checkIndex > -1)
            {
                Bitmap resultPhoto = (Bitmap)ImageUtil.SetBackground(this.resultPhotoList[checkIndex], System.Drawing.Color.White);

                resultPhoto.Save(@"C:\\license_photo.png");
                this.parent.SavePhoto(resultPhoto);
            }
        }

        private void clearChecked()
        {

            this.brd1.BorderBrush = new SolidColorBrush(Color.FromRgb(170, 170, 170));
            this.brd2.BorderBrush = new SolidColorBrush(Color.FromRgb(170, 170, 170));
            this.brd3.BorderBrush = new SolidColorBrush(Color.FromRgb(170, 170, 170));
            this.imgCheckPhoto1.Visibility = Visibility.Hidden;
            this.imgCheckPhoto2.Visibility = Visibility.Hidden;
            this.imgCheckPhoto3.Visibility = Visibility.Hidden;
            
        }

        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ResetIndex();
        }
    }
}
